﻿using System;
using Storyteller.Framework.Math;

namespace Storyteller.Framework.Graphics.Particles.Emitters
{
	public class ConeEmitter : ParticleEmitter
	{
		public float Angle { get; set; }

		public ConeEmitter(int maximumParticles, int particlesPerSecond, Texture texture)
			: base(maximumParticles, particlesPerSecond, texture)
		{
			Angle = MathHelper.PiOver4;
		}

		public ConeEmitter(int maximumParticles, int particlesPerSecond, Texture texture, TextureRegion textureRegion)
			: base(maximumParticles, particlesPerSecond, texture, textureRegion)
		{
		}

		protected override void EmitParticles(int count, Particle[] destination, int offset)
		{
			float rotation = WorldRotation + LocalRotation;
			Vector2 position = WorldPosition + LocalPosition;

			float radians = random.NextFloat(rotation - Angle / 2, rotation + Angle / 2);

			for (int i = 0; i < count; i++)
			{
				Particle p = destination[offset + i];

				p.Position = position;
				p.Velocity = new Vector2((float)System.Math.Cos(radians) * InitialSpeed, (float)System.Math.Sin(radians) * InitialSpeed);

				p.Lifetime = 0;
				p.Scale = InitialScale;
				p.Rotation = InitialRotation;
				p.Color = InitialColor;
			}
		}
	}
}
